package com.zhongxi.guoyuan.utils;

import android.content.Context;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.TextAppearanceSpan;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;

/**
 * Created by Administrator on 2016/10/19.
 */

public class MyTextUtils {
    /**
     * 格式化刷文字
     *
     * @param context
     * @param str
     * @param start
     * @param end
     * @return
     */
    public static SpannableString tvStyle(Context context, String str, int start, int end) {
        SpannableString styleText = new SpannableString(str);
        styleText.setSpan(new TextAppearanceSpan(context, R.style.tvstyle), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return styleText;
    }

    /**
     * 避免多次提交
     */
    private static long lastClickTime;

    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (0 < timeD && timeD < 2000) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    public static void TextChangeUtils(Context mContext, TextView tvNum, Editable content, int totalNum) {
        int length = content.length();
        int end = MyTextUtils.num(length);
        String str = length + "/" + totalNum;
        SpannableString ss = MyTextUtils.stringColorSet(mContext, str, 0, end);
        tvNum.setText(ss);
    }

    public static int num(int length) {
        int end = 0;
        if (length != 0) {
            if (length > 99) {
                end = 3;
            } else if (length > 9) {
                end = 2;
            } else {
                end = 1;
            }
        }
        return end;
    }

    public static SpannableString stringColorSet(Context context, String str, int startIndex, int endIndex) {
        SpannableString ss = new SpannableString(str);
        ss.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.red)), startIndex, endIndex, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
        return ss;
    }

   /* public static SpannableString stringColor(Context context, String str, int startIndex[], int endIndex[]){
        SpannableString ss = new SpannableString(str);
        ss.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.red)), startIndex[0], endIndex[0], SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
        ss.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.red)), startIndex[1], endIndex[1], SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
        return ss;
    }*/

}
